const mongoose = require('mongoose');

const exerciseSchema = new mongoose.Schema({
  cardId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Card',
    required: true
  },
  question: {
    type: String,
    required: true
  },
  type: {
    type: String,
    enum: ['choice', 'fill', 'code'],
    required: true
  },
  options: [{
    id: String,
    text: String
  }],
  answer: {
    type: mongoose.Schema.Types.Mixed, // 可以是字符串、数组或对象，取决于题目类型
    required: true
  },
  difficulty: {
    type: Number,
    required: true,
    min: 1,
    max: 5
  },
  explanation: {
    type: String,
    required: true
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

// 更新时间中间件
exerciseSchema.pre('save', function(next) {
  this.updatedAt = Date.now();
  next();
});

const Exercise = mongoose.model('Exercise', exerciseSchema);

module.exports = Exercise; 